(function() {
    tinymce.create('tinymce.plugins.medialibraryPlugin', {
        init : function(ed, url) {
            var vp = tinymce.DOM.getViewPort(),
                H = vp.h - 80, W = ( 640 < vp.w ) ? 640 : vp.w;

            ed.addCommand('mceMediaInsert', function() {
                //tb_show('媒体库', '/admin/media/upload?tab=gallery&TB_iframe=true&width=' + W + '&height:' + H);
                $('#__medialibrary').trigger('click');
            });
            ed.addButton('medialibrary', {
                title : '媒体库',
                cmd : 'mceMediaInsert',
                //image : url + '/img/download.gif'
            });

            tinymce.DOM.add(document.body, 'a', {
                'id' : '__medialibrary',
                'style' : 'display:none;',
                'href' : '/admin/media/upload?tab=gallery&mce=true',
            });
            // 注册媒体库弹窗
            if ($.fancybox) {
                $('#__medialibrary').fancybox({
                    'autoScale' : true,
                    'type' : 'iframe',
                    'width' : 670,
                    'height' : 388
                });
            } else {
                alert('fancybox is not defined.');
            }
        },
        
        createControl : function(n, cm) {
            return null;
        },

        getInfo : function() {
            return {
                longname : 'Xunbaotu Medialibrary',
                author : 'Ameng',
                authorurl : 'http://uimeet.com',
                infourl : 'http://uimeet.com',
                version : '0.1b'
            };
        }
    });
    tinymce.PluginManager.add('medialibrary', tinymce.plugins.medialibraryPlugin);
})();

function insert_to_editor(media_id, title, source_url, thumbnail_url, class_) {
    send_to_editor('<a href="' + source_url + '" target="_blank"><img class="media-' + media_id + ' ' + class_ + '" src="' + thumbnail_url + '" title="'+ title +'" /></a>');
    if ($.fancybox) {
        $.fancybox.close();
    }
};
